<?php 
include_once('databaseAccess.php');

$pages = array();

// generate Quiz pages
for($i=0; $i < count( $questionCategoryArray ); $i++){
	$tmpJSON = array();
	//pageID
	$tmpJSON['pageID'] = getQuizID($i);
	//nextPageID
	$tmpJSON['nextPageID'] = "quiz".$i."LinkID";
	//nextAHRef
	$tmpJSON['nextAHRef'] = getQuizID(($i+1));
	//question
	$tmpJSON['question'] = htmlentities( getQuestionQuest( $questionCategoryArray[$i]['questnr'] ) );
	
	$myAnswers = getAnswersAnswArray( $questionCategoryArray[$i]['questnr'] );
	$answerRadioStringList = array();
	
	$tmpAnswersJSON = array();
	for($y = 0; $y < count( $myAnswers ); $y++ ) {
		$tmpOneAnswerJSON = array();
		if($y == 0) {
			$tmpOneAnswerJSON['radioID'] = "radio".$i.$y;
			$tmpOneAnswerJSON['answer'] = htmlentities( $myAnswers[$y] );
			$tmpAnswersJSON[$y] = $tmpOneAnswerJSON;
		} 
		else {
			$tmpOneAnswerJSON['radioID'] = "radio".$i.$y;
			$tmpOneAnswerJSON['answer'] = htmlentities( $myAnswers[$y] );
			$answerRadioStringList[$y-1] = $tmpOneAnswerJSON;
		}
	}
	$randomAgain = true;
	$localeZufallsZahl = 0;
	$tmpArrayList = array();
	for($y=0; $y<count($answerRadioStringList); $y++)
	{
		$randomAgain = true;
		while($randomAgain) {
			$randomAgain = false;
			$localeZufallsZahl = rand(0, count($answerRadioStringList)-1);

			for($z=0; $z < count( $tmpArrayList); $z++)
			{
				if($tmpArrayList[$z] == $localeZufallsZahl) {
					$randomAgain = true;
				}
			}
		}
		$tmpArrayList[$y] = $localeZufallsZahl;
	}
	for($y = 0; $y < count( $answerRadioStringList ); $y++ ) {
		$tmpAnswersJSON[$y+1] = $answerRadioStringList[ $tmpArrayList[$y] ];
	}
	//answers
	$tmpJSON['answers'] = $tmpAnswersJSON;
	$pages[$i] = $tmpJSON;
}


$result = json_encode($pages);
echo $result;	

?>